package com.onpositive.gamemaker.editor;

import java.io.File;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
/**
 * This class provides some methods wrapping frequently needed Editor/Workbench UI actions
 * @author 32kda
 *
 */
public class EditorUtil
{
	/**
	 * Used to get currently active editor
	 * @return Active editor, if present.
	 * <code>null</code>, if no active editor/workbench page or workbench window present
	 */
	public static IEditorPart getActiveEditor()
	{
		IWorkbenchPage activePage = getActivePage();
		if (activePage != null)
			return activePage.getActiveEditor();
		return null;
	}

	/**
	 * Return currently active {@link IWorkbenchPage}
	 * @return currently active {@link IWorkbenchPage}, or <code>null</code> if none
	 */
	public static IWorkbenchPage getActivePage()
	{
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (window == null && PlatformUI.getWorkbench().getWorkbenchWindows().length > 0)
			window = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
		if (window == null)
			return null;
		IWorkbenchPage activePage = window.getActivePage();
		if (activePage == null && window.getPages().length > 0)
			activePage = window.getPages()[0];
		if (activePage == null)
			return null;
		return activePage;
	}
	
	/**
	 * Opens editor for some external file
	 * @param path Absolute path to that file
	 */
	public static void openEditorForFile(String path)
	{
		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (workbenchWindow == null)
			workbenchWindow = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
		if (workbenchWindow == null)
		{
			GameCreatorEditorPlugin.logError("No active window");
			return;
		}
		

		File file = new File(path);
		if (!file.exists())
		{
			GameCreatorEditorPlugin.logError("File not found - " + path);
			return;
		}
		openEditorForFile(file,null);
	}
	
	/**
	 * Opens editor for some external file
	 * @param file {@link File} object representing that file
	 * @param editorId Wanted editor id or <code>null</code> for autofinding
	 * @return 
	 */
	public static IEditorPart openEditorForFile(File file, String editorId)
	{
		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		if (workbenchWindow == null)
			workbenchWindow = PlatformUI.getWorkbench().getWorkbenchWindows()[0];
		if (workbenchWindow == null)
		{
			GameCreatorEditorPlugin.logError("No active window");
			return null;
		}
		IWorkbenchPage page = workbenchWindow.getActivePage();
		if (page == null)
			page = workbenchWindow.getPages()[0];
		if (page == null)
		{
			GameCreatorEditorPlugin.logError("No active page");
			return null;
		}
		try
		{
			IWorkspace ws = ResourcesPlugin.getWorkspace();
			IProject project = ws.getRoot().getProject("External Files");
			if (!project.exists())
			    project.create(null);
			if (!project.isOpen())
			    project.open(null);
/*			Shell shell = workbenchWindow.getShell();
			if (shell == null)
				shell = Display.getDefault().getActiveShell();		*/
			IPath location = new Path(file.getAbsolutePath());
			IFile inputFile = project.getFile(location.lastSegment());
			inputFile.createLink(location, IResource.REPLACE, null);
			if (page != null)
			{
				if (editorId == null)
					return IDE.openEditor(page, inputFile );
				else
					return IDE.openEditor(page, inputFile, editorId);
			}
		} catch (CoreException e)
		{
			GameCreatorEditorPlugin.logError(e);
		}
		return null;
	
	}
	
}
